package vmcs.customer.state;

import vmcs.common.VMCSException;
import vmcs.controller.TransactionController;

public class EnoughCoinsInsertedState implements State{

	private TransactionController transactionController;
	
	private static EnoughCoinsInsertedState instance;
	
	private EnoughCoinsInsertedState(TransactionController transactionController) {
		this.transactionController = transactionController; 
	}
	
	public static EnoughCoinsInsertedState getInstance(TransactionController transactionController) {
		if (instance == null) 
			instance = new EnoughCoinsInsertedState(transactionController);
		return instance;
	}

	@Override
	public void selectDrink(int selectedIdx) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispenseDrink(int drinkSelected) {
		boolean drinkDispensed = false;
		try {
			drinkDispensed = transactionController.getDispenseController().dispenseDrink(drinkSelected);
		} catch (VMCSException e) {
			
			if(!drinkDispensed){
				transactionController.terminateMachine();
				return;
			}
		
		}
		transactionController.setState(transactionController.getDrinkDispenseState());
		transactionController.giveChange();
	}

	@Override
	public void giveChange(int changeRequired) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void storeCash() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void insertCoin(int value) {
		// TODO Auto-generated method stub
		
	} 

	@Override
	public void terminateMachine() {
	
			transactionController.getDispenseController().allowSelection(false);

			transactionController.getCoinReceiver().stopReceive();

			transactionController.getCoinReceiver().refundCash();  

			transactionController.setState(transactionController.getMachineTerminatedState());
		
	} 
	
	
}
